package leetcode.editor.cn;

public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dump = new ListNode(-1);
        dump.next = head;
        ListNode delNode = findFromEnd(dump, n+1);
        delNode.next = delNode.next.next;
        return dump.next;
    }

    private ListNode findFromEnd(ListNode head, int n) {
        ListNode p1 = head;
        for (int i=0;i<n;i++){
            p1 = p1.next;
        }
        ListNode p2 = head;
        while (p1!=null){
            p1 = p1.next;
            p2 = p2.next;
        }
        return p2;
    }


    public boolean validPalindrome(String s) {
        int low=0, high = s.length()-1;
        while (high > low) {
            char c1 = s.charAt(low), c2= s.charAt(high);
            if (c1 == c2){
                high--;
                low++;
            }else {
                return validPalindrome(s, low, high -1) || validPalindrome(s, low+1, high);
            }
        }
        return true;
    }

    public boolean validPalindrome(String s, int low, int high) {
        for (int i = low, j = high; i < j; ++i, --j) {
            char c1 = s.charAt(i), c2 = s.charAt(j);
            if (c1 != c2) {
                return false;
            }
        }
        return true;
    }

}
